package com.yisu.kabao.manager.core.service.impl;

import com.yisu.kabao.ewf.common.web.EasyUIGrid;
import com.yisu.kabao.ewf.entity.GoodsInfo;
import com.yisu.kabao.manager.core.mapper.GoodsInfoMapper;
import com.yisu.kabao.manager.core.service.IGoodsInfoService;
import com.yisu.kabao.manager.core.service.impl.func.RedisClusterClient;
import com.yisu.kabao.manager.domain.request.baseInfoPackage.GoodsInfoRequest;
import com.yisu.kabao.manager.domain.vo.baseInfoPackageVos.GoodsExpandInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description： 商品信息接口实现
 * createTime：2017/8/10
 */
@Service("IGoodsTypeService")
public class GoodsInfoServiceImpl extends BaseService implements IGoodsInfoService {
    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    /**
     * author:赵杰
     * Business Description : 分页获取商品信息
     *
     * @param goodsInfoRequest 分页请求信息
     * @return 商品分类列表
     */
    @Override
    public EasyUIGrid<GoodsExpandInfoVo> getGoodsInfoByPage(GoodsInfoRequest goodsInfoRequest) {
        List<GoodsExpandInfoVo> goodsInfoArrays = goodsInfoMapper.getGoodsInfoByPage(goodsInfoRequest);
        EasyUIGrid<GoodsExpandInfoVo> gridData = new EasyUIGrid<>();
        gridData.setTotal(goodsInfoRequest.getTotalRecords());
        gridData.setRows(goodsInfoArrays);
        return gridData;
    }

    /**
     * author:赵杰
     * Business Description : 通过主键ID，获取商品信息
     *
     * @param primaryKey 主键
     * @return
     */
    @Override
    public GoodsInfo getGoodsInfo(long primaryKey) {
        return goodsInfoMapper.selectByPrimaryKey(primaryKey);
    }

    /**
     * author:赵杰
     * Business Description : 编辑商品信息
     *
     * @param goodsInfo 商品信息
     * @return
     */
    @Override
    public boolean modifyGoodsInfo(GoodsInfo goodsInfo) {
        try {
            GoodsInfo checkModel = goodsInfoMapper.selectByPrimaryKey(goodsInfo.getId());
            if (checkModel == null) {
                goodsInfo.setCreateDate(new Date());
                goodsInfo.setIsValid(true);
                return goodsInfoMapper.insertSelective(goodsInfo) > 0;
            } else {
                GoodsInfo updateModel = new GoodsInfo();
                updateModel.setUpdateDate(new Date());
                updateModel.setGoodsTypeId(goodsInfo.getGoodsTypeId());
                updateModel.setName(goodsInfo.getName());
                updateModel.setImages(goodsInfo.getImages());
                updateModel.setSalesNo(goodsInfo.getSalesNo());
                updateModel.setSalesType(goodsInfo.getSalesType());
                updateModel.setStock(goodsInfo.getStock());
                updateModel.setTerm(goodsInfo.getTerm());
                updateModel.setStatus(goodsInfo.getStatus());
                updateModel.setPrice(goodsInfo.getPrice());
                updateModel.setIsTen(goodsInfo.getIsTen());
                updateModel.setIsRechargeableCard(goodsInfo.getIsRechargeableCard());
                updateModel.setSort(goodsInfo.getSort());
                updateModel.setDetail(goodsInfo.getDetail());
                updateModel.setId(goodsInfo.getId());
                return goodsInfoMapper.updateByPrimaryKeySelective(updateModel) > 0;
            }
        } catch (Exception e) {

        }
        return false;
    }

    /**
     * author:赵杰
     * Business Description : 通过主键ID，删除商品信息
     *
     * @param primaryKey 主键ID
     * @return
     */
    @Override
    public boolean deleteGoodsInfo(long primaryKey) {
        try {
            GoodsInfo goodsInfo = goodsInfoMapper.selectByPrimaryKey(primaryKey);
            if (goodsInfo != null) {
                GoodsInfo updateModel = new GoodsInfo();
                updateModel.setId(primaryKey);
                updateModel.setIsValid(false);
                goodsInfoMapper.updateByPrimaryKeySelective(updateModel);
            }
        } catch (Exception e) {
            logger.error("通过主键ID，删除商品信息异常：" + e.getMessage());
            return false;
        }
        return true;
    }
}
